﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_detailOrder : System.Web.UI.Page
{
    DAOOrder dao_order = new DAOOrder();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }
    protected void LoadData()
    {

        int OrderId = Convert.ToInt32(Request.QueryString["orderid"].ToString());
        String Command = Request.QueryString["command"].ToString();
        Order order = dao_order.GetOrderById(OrderId);
        double TongTien = 0.0;
        order.ListProduct = dao_order.GetOrderDetailById(OrderId);
        foreach (var item in order.ListProduct)
        {
            TongTien = TongTien + item.Price;
        }

        if (Command.Equals("detail"))
        {
            txtOrderId.Text = order.OrderId.ToString();
            txtUserName.Text = order.UserName;
            txtDate.Text = order.OrderDate.ToString("dd-MM-yyyy");
            txtNameOfReceiver.Text = order.NameOfReceiver;
            txtAddressOfReceiver.Text = order.AddressOfReceiver;
            txtPhoneOfReceiver.Text = order.PhoneOfReceiver;
            chbxStatus.Checked = order.Status;
            txtTongTien.Text = TongTien.ToString();
            gvOrderProduct.DataSource = order.ListProduct;
            gvOrderProduct.DataBind();

            btnUpdate.Visible = btnCancel.Visible = false;
        }


    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        btnEdit.Visible = false;
        btnUpdate.Visible = btnCancel.Visible = true;
        chbxStatus.Enabled = true;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        btnEdit.Visible = true;
        btnUpdate.Visible = btnCancel.Visible = false;
        chbxStatus.Enabled = false;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Boolean Status = chbxStatus.Checked;
        int OrderId = Convert.ToInt32(txtOrderId.Text);
        int kq = dao_order.UpdateStatusOrder(Status,OrderId);
        if (kq > 0)
        {
            Message.Text = "Cập nhật trạng thái thành công";
        }
        else {
            Message.Text = "Cập nhật trạng thái KHÔNG thành công";
        }
        btnEdit.Visible = true;
        btnUpdate.Visible = btnCancel.Visible = false;
        chbxStatus.Enabled = false;

    }
}